package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.RecordService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Record;
import com.zhongkai.model3.Record2;
import com.zhongkai.model3.Record3;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 库存操作记录
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RecordServiceImpl implements RecordService {


    @Autowired
    private IBaseDao iBaseDao;


    @Autowired
    private PageQuerDao pageQuerDao;

    @Override
    public ZhongkaiResult listRecord(Integer page, Integer rows) { //工程
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Record.class);
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }

    @Override
    public ZhongkaiResult listRecord2(Integer page, Integer rows) {  //办公
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Record2.class);
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }

    @Override
    public ZhongkaiResult listRecord3(Integer page, Integer rows) { //产品
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Record3.class);
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }


    /**
     * 库存操作记录查询
     *
     * @param page 页数
     * @param rows 每页数据条数
     * @param status 类型 //1工程 2办公 3公司产品
     * @param name 操作人员姓名
     * @param time 时间
     */
    @Override
    public ZhongkaiResult seekRecord(Integer page, Integer rows, String status,String name, Long time) {
        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(status)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
        DetachedCriteria detachedCriteria;
        switch (status){
            case "1":
                detachedCriteria = DetachedCriteria.forClass(Record.class);
                break;

            case "2":
                detachedCriteria = DetachedCriteria.forClass(Record2.class);
                break;

            case "3":
                detachedCriteria = DetachedCriteria.forClass(Record3.class);

                break;

            default:

                return ZhongkaiResult.build(400, "参数不合法");
        }

        if (!StringUtils.isNullOrEmpty(name)){
            detachedCriteria.add(Restrictions.eq("recordUser", name)); //增加条件
        }

        if (time != 0){
            DateTime now = new DateTime(time);
            long millis = now.dayOfWeek().roundCeilingCopy().getMillis();//明天00点的时间戳
            long floor = now.dayOfWeek().roundFloorCopy().getMillis();//今天00点的时间戳

            detachedCriteria.add(Restrictions.gt("recordTime", floor)); //增加条件
            detachedCriteria.add(Restrictions.lt("recordTime", millis)); //增加条件
        }

        pageBean.setDetachedCriteria(detachedCriteria);
        pageQuerDao.pageQuery(pageBean);

        return ZhongkaiResult.ok(pageBean);
    }
}
